#include <string>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <sstream>

using namespace std;

//1、创建字符串
void create_str()
{
	string s1 = "hello world1";
	string s2("hello world2");
	string s3 = s2;
	string s4(10, 'a');//10个a
	std::cout << s1 << endl;
	std::cout << s2 << endl;
	std::cout << s3 << endl;
	std::cout << s4 << endl;
}
//2、遍历字符串
void bianli_string()
{
	string s1 = "abcdefg";
	//1、数组方式(如果越界不会往外抛异常,程序直接停止)
	for (int i = 0; i < s1.length(); i++)
	{
		cout << s1[i] << "\t";
	}
	cout << endl;
	//2、迭代器(一个指针，指向一个具体的元素)
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	//3、函数(如果越界会向外抛异常)
	try
	{
		for (int i = 0; i < s1.length(); i++)
		{
			cout << s1.at(i) << " ";
		}
	}
	catch (...)
	{
		cout << "异常发生了" << endl;
	}

}

//3、字符指针和string的转换
void pointer_string()
{
	string s1 = "hello world";
	//1、获取字符串的指针
	printf("s1:%s\n", s1.c_str());
	char buf[5] = { 0 };
	//2、将字符串拷贝到字符数组中
	s1.copy(buf, 4, 1);
	buf[4] = '\0';//c语言中的字符串以'\0'结尾
	printf("%s\n", buf);
	cout << buf << endl;
}

//4、字符串的连接
void connect_string()
{
	string s1 = "hello ";
	string s2 = "world";
	//两种方式
	string s3 = s1 + s2;
	string s4 = s1.append(s2);
	cout << s3 << endl;
	cout << s4 << endl;

	//用流
	ostringstream out;
	out << s1 << s2 << endl;
	string s5 = out.str();
	cout << s5 << endl;

}

//5、字符串的比较
void compare_string()
{
	string s1 = "hello";
	string s2 = "world";
	int result1 = s2.compare(s1);//1
	int result2 = s2.compare(s2);//0
	int result3 = s1.compare(s2);//-1
	cout << result1 << endl;
	cout << result2 << endl;
	cout << result3 << endl;
}
//6、子串
void sub_string()
{
	string s1 = "my name is Peter";
	string s2 = s1.substr(1, 6); //从1开始取6个字符
	cout << s2 << endl;
}

//7、查找和替换
void find_replace_string()
{
	string s1 = "Hello,how are you,nice to meet you jhi";
	//1、查找
	int index = s1.find("are", 0);//从0开始往后找
	cout << index << endl;


	//2、查找某个字符串出现的次数以及每次出现的位置
	int offset = s1.find("you", 0);//第一次从0开始找
	int num = 0;
	while (offset != string::npos)//没有找完
	{
		cout << "位置:" << offset << endl;
		num++;
		offset = s1.find("you", offset + 1);//继续找，从当前位置的后一个位置开始往后找,如果找到最后一个字符串还是没找到，跳出循环
	}
	cout << "次数:" << num << endl;

	//3、替换(先查找后替换)将 you 换成 YOU
	int pos = s1.find("you", 0);
	while (pos != string::npos)
	{
		s1 = s1.replace(pos, 3, "YOU");
		pos = s1.find("you", pos + 1);
	}

	cout << "替换结果:" << s1 << endl;
}

//8、截断和插入
void delete_insert_string()
{
	string s1 = "hello world hello how are you nice to meet you";
	//删除 you
	for (int index = s1.find("you", 0); index != string::npos; index = s1.find("you", 0))
	{
		s1 = s1.erase(index, 3);//删除you
		s1 = s1.insert(index, "me");//插入me(在index的前面插入)
	}
	cout << "删除后的字符串:" << s1 << endl;

	//在头部插入
	s1 = s1.insert(0, "头部");
	//在尾部插入
	s1 = s1.insert(s1.length(), "尾部");
	cout << s1 << endl;
	//删除整个字符串
	s1.erase(s1.begin(), s1.end());
	cout << s1 << s1.length() << endl;

}

//9、转换 (头文件:string, cctype,algorithm)
void translate_string()
{
	string s1 = "AAABBccc";
	//转大写
	transform(s1.begin(), s1.end(), s1.begin(), toupper);
	cout << s1 << endl;
	//转小写
	transform(s1.begin(), s1.end(), s1.begin(), tolower);
	cout << s1 << endl;
}
void main2()
{
	system("pause");
}